﻿using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UtilsORM;
using UtilsORM.MySqlData;

namespace ServiceWeb.DBGateway
{
    public class DBIotDeviceVariable
    {
        public DBIotDeviceVariable()
        {

        }
        EFYihongIotContext ef = new EFYihongIotContext();
        /// <summary>
        /// 获取有配置变量值的设备,有条件触发能力
        /// </summary>
        /// <param name="clientGuid"></param>
        /// <param name="export">0可上传数据值，1可执指令</param>
        /// <returns></returns>
        public List<iot_device> getHasVariableDevice(string clientGuid)
        {

            string sql = "select * from iot_device where d_model_guid in(select mv_model_guid from iot_device_model_variable inner join iot_device_model_variable_relation on mv_v_guid=v_guid) ";

            sql = sql + " and  d_status=0 and  d_c_guid in('" + clientGuid + "','00000000-0000-0000-0000-000000000000') ";
            return MySqlYihongIot.GetList<iot_device>(sql);
        }
        public List<iot_device> getHasVariableDevice(string clientGuid, string tcode)
        {

            string sql = "select * from iot_device where d_model_guid in(select mv_model_guid from iot_device_model_variable inner join iot_device_model_variable_relation on mv_v_guid=v_guid)  ";
            if (!string.IsNullOrWhiteSpace(tcode))
            {
                //指定类型传感器
                sql = sql + " and d_model_guid in(select m_guid from iot_device_model inner join iot_device_model_type on m_type_guid=t_guid where t_code='" + tcode + "') ";
            }
            sql = sql + " and  d_status=0 and  d_c_guid in('" + clientGuid + "','00000000-0000-0000-0000-000000000000')  ";
            return MySqlYihongIot.GetList<iot_device>(sql);
        }
        public List<iot_device> getHasVariableDevice()
        {

            string sql = "select * from iot_device where d_model_guid in(select mv_model_guid from iot_device_model_variable inner join iot_device_model_variable_relation on mv_v_guid=v_guid)   ";

            sql = sql + "  and  d_status=0 ";
            return MySqlYihongIot.GetList<iot_device>(sql);
        }
        /// <summary>
        /// 获取设置变量能力
        /// </summary>
        /// <param name="clientGuid"></param>
        /// <param name="dguid"></param>
        /// <param name="export">0可上传数据值，1可执指令</param>
        /// <returns></returns>
        public List<iot_device_model_variable> getVariableByDevice(string clientGuid, string dguid, int export)
        {
            string sql = "select  v.*  from iot_device_model_variable v  inner join iot_device_model_variable_relation on mv_v_guid=v_guid inner join iot_device on mv_model_guid=d_model_guid where  d_guid='" + dguid + "'";

            return MySqlYihongIot.GetList<iot_device_model_variable>(sql);
        }


        public iot_device_model_variable getVariable(string vguid)
        {
            return ef.iot_device_model_variable.Where(c => c.v_guid == vguid).FirstOrDefault();
        }

        /// <summary>
        /// 获取设置变量能力
        /// </summary>
        /// <param name="clientGuid"></param>
        /// <param name="export">0可上传数据值，1可执指令</param>
        /// <param name="dvguid"></param>
        /// <returns></returns>
        public List<iot_device_model_variable> getVariableByGuid(string clientGuid, string vguid, int export)
        {
            string sql = "select a.* from iot_device_model_variable a inner join iot_device_model_variable_relation on mv_v_guid=v_guid inner join iot_device on mv_model_guid=d_model_guid where   v_guid='" + vguid + "'";

            return MySqlYihongIot.GetList<iot_device_model_variable>(sql);
        }
    }
}
